const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/database');

class Refund extends Model {}

Refund.init({
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  payment_id: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'payments',
      key: 'id'
    },
    comment: '关联支付ID'
  },
  refund_no: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    comment: '退款单号'
  },
  amount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '退款金额（单位：分）'
  },
  reason: {
    type: DataTypes.STRING,
    comment: '退款原因'
  },
  status: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: 'pending',
    comment: '退款状态：pending/processing/completed/failed'
  },
  refund_id: {
    type: DataTypes.STRING,
    comment: '支付平台退款单号'
  },
  refunded_at: {
    type: DataTypes.DATE,
    comment: '退款完成时间'
  },
  response: {
    type: DataTypes.JSON,
    comment: '支付平台响应数据'
  },
  created_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '更新时间'
  }
}, {
  sequelize,
  modelName: 'Refund',
  tableName: 'refunds',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      fields: ['payment_id'],
      comment: '关联支付ID索引'
    },
    {
      fields: ['refund_no'],
      unique: true,
      comment: '退款单号索引'
    },
    {
      fields: ['refund_id'],
      comment: '支付平台退款单号索引'
    },
    {
      fields: ['status'],
      comment: '退款状态索引'
    }
  ],
  comment: '退款信息表'
});

module.exports = Refund; 